<template>
  <div class="style-page">
    <div class="head-page-title">{{ title }}样式配置</div>
    <div class="line"></div>

    <div class="component-style">
      <div class="header" @click="contentCollapsed = !contentCollapsed">
        <p class="title">内容样式</p>
        <el-icon v-if="!contentCollapsed"><ArrowDown /></el-icon>
        <el-icon v-else><ArrowRight /></el-icon>
      </div>

      <div v-if="!contentCollapsed">
        <el-form :model="form" label-width="auto">
          <el-form-item label="第一行圆形按钮背景色：">
            <el-color-picker v-model="form.practiceCircleBgColor"></el-color-picker>
            <el-input style="width: 160px; margin-left: 10px" v-model="form.practiceCircleBgColor">
              <template #append>
                <el-button @click="form.practiceCircleBgColor = '#1DC108'">重置</el-button>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="第二行圆形按钮背景色：">
            <el-color-picker v-model="form.examCircleBgColor"></el-color-picker>
            <el-input style="width: 160px; margin-left: 10px" v-model="form.examCircleBgColor">
              <template #append>
                <el-button @click="form.examCircleBgColor = '#1DC108'">重置</el-button>
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="按钮形状：">
            <el-radio-group v-model="form.borderRadius">
              <el-radio label="正常" :value="0" />
              <el-radio label="圆角" :value="12" />
              <el-radio label="圆形" :value="100" />
            </el-radio-group>
          </el-form-item>
        </el-form>

        <div class="section-title">标题设置</div>
        <el-form label-width="auto">
          <el-form-item label="第一行主标题：">
            <el-input v-model="showComponentData.practiceHeader.title" />
          </el-form-item>
          <el-form-item label="第一行副标题：">
            <el-input v-model="showComponentData.practiceHeader.subTitle" />
          </el-form-item>
          <el-form-item label="第二行主标题：">
            <el-input v-model="showComponentData.examHeader.title" />
          </el-form-item>
          <el-form-item label="第二行副标题：">
            <el-input v-model="showComponentData.examHeader.subTitle" />
          </el-form-item>
          <el-form-item label="主标题字体大小：">
            <div class="flex">
              <el-slider
                style="width: 70%"
                v-model="form.headerStyle.titleStyle.fontSize"
                :min="12"
                :max="50"
              />
              <el-input
                type="number"
                style="width: 30%; margin-left: 20px"
                v-model.number="form.headerStyle.titleStyle.fontSize"
                :min="12"
                :max="50"
              >
                <template #append>px</template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item label="字体颜色：">
            <el-color-picker v-model="form.headerStyle.titleStyle.color"></el-color-picker>
            <el-input
              style="width: 160px; margin-left: 10px"
              v-model="form.headerStyle.titleStyle.color"
            >
              <template #append>
                <el-button @click="form.headerStyle.titleStyle.color = '#1DC108'">重置</el-button>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="字体粗细：">
            <el-radio-group v-model="form.headerStyle.titleStyle.fontWeight">
              <el-radio label="正常" value="normal" />
              <el-radio label="加粗" value="bold" />
            </el-radio-group>
          </el-form-item>

          <el-form-item label="副标题字体大小：">
            <div class="flex">
              <el-slider
                style="width: 70%"
                v-model="form.headerStyle.subTitleStyle.fontSize"
                :min="12"
                :max="50"
              />
              <el-input
                type="number"
                style="width: 30%; margin-left: 20px"
                v-model.number="form.headerStyle.subTitleStyle.fontSize"
                :min="12"
                :max="50"
              >
                <template #append>px</template>
              </el-input>
            </div>
          </el-form-item>
          <el-form-item label="字体颜色：">
            <el-color-picker v-model="form.headerStyle.subTitleStyle.color"></el-color-picker>
            <el-input
              style="width: 160px; margin-left: 10px"
              v-model="form.headerStyle.subTitleStyle.color"
            >
              <template #append>
                <el-button @click="form.headerStyle.subTitleStyle.color = '#1DC108'"
                  >重置</el-button
                >
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="字体粗细：">
            <el-radio-group v-model="form.headerStyle.subTitleStyle.fontWeight">
              <el-radio label="正常" value="normal" />
              <el-radio label="加粗" value="bold" />
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>

      <div class="component-style">
        <div class="header" @click="practiceCollapsed = !practiceCollapsed">
          <p class="title">第一行按钮</p>
          <el-icon v-if="!practiceCollapsed"><ArrowDown /></el-icon>
          <el-icon v-else><ArrowRight /></el-icon>
        </div>

        <div v-if="!practiceCollapsed">
          <VueDraggable
            v-model="showComponentData.practice_question_list"
            :animation="150"
            ghostClass="ghost"
            class="content"
            :filter="'.el-input'"
            :preventOnFilter="false"
            @end="handleUpdateComponent"
          >
            <div
              v-for="(item, index) in showComponentData.practice_question_list"
              :key="item.id"
              class="carousel-item"
            >
              <div class="upload">
                <div class="avatar-uploader" @click="handleChangeImage(index, 'practice')">
                  <el-image v-if="item.img" :src="item.img" class="avatar" fit="fill"></el-image>
                  <i v-else class="el-icon-plus avatar-uploader-icon"
                    ><el-icon> <Plus /> </el-icon
                  ></i>
                  <div v-if="item.img" class="mask">替换</div>
                  <el-icon
                    v-if="item.img"
                    class="del-image"
                    :size="20"
                    @click.stop="handleDelPracticeImg(index)"
                    ><CircleCloseFilled
                  /></el-icon>
                </div>
              </div>
              <div class="operation">
                <div class="column">
                  <p class="title">按钮名称</p>
                  <el-input v-model="item.name" maxlength="6" show-word-limit></el-input>
                </div>
                <div class="column">
                  <p class="title">跳转链接</p>
                  <div class="link">
                    <el-icon style="color: var(--el-color-primary)"><Link /></el-icon>
                    <el-link type="primary" @click="handleOpenLinkDialog(index, 'practice')">{{
                      item.link?.name || '请选择跳转链接'
                    }}</el-link></div
                  >
                </div>
              </div>

              <el-icon class="del-icon" :size="16" color="red" @click="handleDelPractice(index)"
                ><CircleCloseFilled
              /></el-icon>
            </div>

            <div>
              <el-tag type="primary" class="submit-btn" @click="handleAddPractice"
                >添加按钮({{ showComponentData.practice_question_list.length }}/5)</el-tag
              >
            </div>
          </VueDraggable>
        </div>

        <div class="header" @click="examCollapsed = !examCollapsed">
          <p class="title">第二行按钮</p>
          <el-icon v-if="!examCollapsed"><ArrowDown /></el-icon>
          <el-icon v-else><ArrowRight /></el-icon>
        </div>

        <div v-if="!examCollapsed">
          <VueDraggable
            v-model="showComponentData.exam_question_list"
            :animation="150"
            ghostClass="ghost"
            class="content"
            :filter="'.el-input'"
            :preventOnFilter="false"
            @end="handleUpdateComponent"
          >
            <div
              v-for="(item, index) in showComponentData.exam_question_list"
              :key="item.id"
              class="carousel-item"
            >
              <div class="upload">
                <div class="avatar-uploader" @click="handleChangeImage(index, 'exam')">
                  <el-image v-if="item.img" :src="item.img" class="avatar" fit="fill"></el-image>
                  <i v-else class="el-icon-plus avatar-uploader-icon"
                    ><el-icon> <Plus /> </el-icon
                  ></i>
                  <div v-if="item.img" class="mask">替换</div>
                  <el-icon
                    v-if="item.img"
                    class="del-image"
                    :size="20"
                    @click.stop="handleDelExamImg(index)"
                    ><CircleCloseFilled
                  /></el-icon>
                </div>
              </div>
              <div class="operation">
                <div class="column">
                  <p class="title">按钮名称</p>
                  <el-input v-model="item.name" maxlength="6" show-word-limit></el-input>
                </div>
                <div class="column">
                  <p class="title">跳转链接</p>
                  <div class="link">
                    <el-icon style="color: var(--el-color-primary)"><Link /></el-icon>
                    <el-link type="primary" @click="handleOpenLinkDialog(index, 'exam')">{{
                      item.link?.name || '请选择跳转链接'
                    }}</el-link></div
                  >
                </div>
              </div>

              <el-icon class="del-icon" :size="16" color="red" @click="handleDelExam(index)"
                ><CircleCloseFilled
              /></el-icon>
            </div>

            <div>
              <el-tag type="primary" class="submit-btn" @click="handleAddExam"
                >添加按钮({{ showComponentData.exam_question_list.length }}/5)</el-tag
              >
            </div>
          </VueDraggable>
        </div>
      </div>
    </div>

    <link-list-dialog
      v-if="showLinkDataDialog"
      :show-link-dialog="showLinkDataDialog"
      link-type="exam"
      @close-dialog="handleCloseLinkDialog"
    ></link-list-dialog>

    <image-selected-dialog
      v-if="showImageChooseDialog"
      :show-image-choose-dialog="showImageChooseDialog"
      :single-type="true"
      :operation-type="false"
      @close-dialog="handleCloseImageDialog"
    />
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive, watch, toRaw } from 'vue'
  import { ElMessage } from 'element-plus'
  import { ArrowDown, ArrowRight, CircleCloseFilled, Link, Plus } from '@element-plus/icons-vue'
  import { VueDraggable } from 'vue-draggable-plus'
  import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'
  import LinkListDialog from '@/views/decoration/diyComponents/LinkListDialog.vue'
  import debounce from 'lodash/debounce'

  const props = defineProps({
    contentData: {
      type: Object,
      default: () => ({})
    },
    title: String
  })
  const emit = defineEmits(['updateData'])

  const showComponentData = ref(props.contentData)
  const showImageChooseDialog = ref(false)
  const showLinkDataDialog = ref(false)
  const contentCollapsed = ref(false)
  const practiceCollapsed = ref(false)
  const examCollapsed = ref(false)
  const changeIndex = ref<number>(-1)
  const addType = ref('')

  const form = reactive({
    borderRadius: 12,
    practiceCircleBgColor: '#5BAAFF',
    examCircleBgColor: '#4edd2d',
    headerStyle: {
      titleStyle: {
        fontSize: 38,
        color: '#000',
        fontWeight: 'bold'
      },
      subTitleStyle: {
        fontSize: 23,
        color: '#ccc',
        fontWeight: 'normal'
      }
    }
  })

  watch(
    form,
    () => {
      emitFormUpdate()
    },
    { deep: true }
  )

  const emitFormUpdate = debounce(() => {
    const raw = toRaw(form)
    showComponentData.value.practiceCircleButton.bgColor = raw.practiceCircleBgColor
    showComponentData.value.examCircleButton.bgColor = raw.examCircleBgColor
    showComponentData.value.imgStyle.borderRadius = raw.borderRadius
    showComponentData.value.headerStyle = raw.headerStyle
    handleUpdateComponent()
  }, 300)

  function handleAddPractice() {
    if (showComponentData.value.practice_question_list.length >= 5) {
      ElMessage.warning('最多添加 5 个图标项')
      return
    }
    showComponentData.value.practice_question_list.push({
      name: '新图标',
      img: ''
    })
    handleUpdateComponent()
  }

  function handleAddExam() {
    if (showComponentData.value.exam_question_list.length >= 5) {
      ElMessage.warning('最多添加 5 个图标项')
      return
    }
    showComponentData.value.exam_question_list.push({
      name: '新图标',
      img: ''
    })
    handleUpdateComponent()
  }

  function handleOpenLinkDialog(index: number, type: string) {
    showLinkDataDialog.value = true
    changeIndex.value = index
    addType.value = type
  }

  function handleCloseLinkDialog(value: any) {
    if (value) {
      if (addType.value === 'practice') {
        showComponentData.value.practice_question_list[changeIndex.value].link = value
      } else if (addType.value === 'exam') {
        showComponentData.value.exam_question_list[changeIndex.value].link = value
      }
    }
    showLinkDataDialog.value = false
  }

  function handleDelPractice(index: number) {
    showComponentData.value.practice_question_list.splice(index, 1)
    handleUpdateComponent()
  }

  function handleDelExam(index: number) {
    showComponentData.value.exam_question_list.splice(index, 1)
    handleUpdateComponent()
  }

  function handleChangeImage(index: number, type: string) {
    changeIndex.value = index
    showImageChooseDialog.value = true
    addType.value = type
  }

  function handleCloseImageDialog(val: any) {
    if (val) {
      if (addType.value === 'practice') {
        showComponentData.value.practice_question_list[changeIndex.value].img = val.fileUrl
      } else if (addType.value === 'exam') {
        showComponentData.value.exam_question_list[changeIndex.value].img = val.fileUrl
      }
      handleUpdateComponent()
    }
    showImageChooseDialog.value = false
  }

  function handleDelPracticeImg(index: number) {
    showComponentData.value.practice_question_list[index].img = ''
    handleUpdateComponent()
  }

  function handleDelExamImg(index: number) {
    showComponentData.value.exam_question_list[index].img = ''
    handleUpdateComponent()
  }

  function handleUpdateComponent() {
    emit('updateData', showComponentData.value)
  }
</script>

<style scoped lang="scss">
  .style-page {
    padding: 20px;

    .head-page-title {
      font-size: 18px;
      font-weight: bold;
      margin-bottom: 10px;
      color: black;
    }

    .section-title {
      font-size: 14px;
      font-weight: bold;
      margin: 20px 0 10px;
      color: black;
    }

    .line {
      height: 1px;
      background-color: #eee;
      margin: 20px 0;
    }

    .drag-area {
      margin-top: 10px;
      margin-bottom: 20px;
    }

    .icon-config-item {
      display: flex;
      align-items: flex-start;
      margin-bottom: 20px;
      border: 1px solid #eee;
      padding: 10px;
      position: relative;

      .img-box {
        width: 70px;
        height: 70px;
        background: #f5f5f5;
        margin-right: 20px;
        cursor: pointer;
        display: flex;
        justify-content: center;
        align-items: center;

        .img-placeholder {
          font-size: 12px;
          color: #999;
        }

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }

      .text-settings {
        flex: 1;
      }

      .delete-icon {
        position: absolute;
        top: 8px;
        right: 8px;
        cursor: pointer;
        color: red;
      }
    }

    .submit-btn {
      width: 100%;
      height: 40px !important;
      cursor: pointer;
      font-size: 14px;
      margin-bottom: 30px;
    }

    .component-style {
      width: 100%;
      margin-top: 20px;

      .header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 20px;

        .title {
          font-size: 14px;
          font-weight: bold;
          color: black;
        }
      }

      .flex {
        display: flex;
        justify-content: space-between;
        width: 100%;
        padding: 0 3px;
      }

      .content {
        .carousel-item {
          width: 100%;
          margin-bottom: 20px;
          display: flex;
          align-items: center;
          position: relative;
          background-color: #f7f8fa;
          padding: 10px;
          box-sizing: border-box;

          &:hover {
            .del-icon {
              display: block;
            }
          }

          .avatar-uploader {
            width: 70px;
            height: 70px;
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            margin-right: 30px;
            text-align: center;

            .mask {
              position: absolute;
              bottom: 0;
              left: 0;
              width: 100%;
              height: 24px;
              background-color: rgba(0, 0, 0, 0.6);
              display: flex;
              align-items: center;
              justify-content: center;
              opacity: 0;
              transition: opacity 0.3s ease;
              color: #fff;
              font-size: 12px;
            }

            .del-image {
              display: none;
              position: absolute;
              top: 0;
              right: 0;
              cursor: pointer;
              color: #8c939d;
            }

            &:hover .mask {
              opacity: 1;
            }

            &:hover .del-image {
              display: block;
            }
          }

          .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 70px;
            height: 70px;
            line-height: 70px;
          }

          .avatar {
            width: 100%;
            height: 70px;
            display: block;
          }

          .del-icon {
            display: none;
            position: absolute;
            top: -2px;
            right: -2px;
            cursor: pointer;
            color: #8c939d;
          }

          .operation {
            flex: 1;

            .column {
              display: flex;
              align-items: center;
              margin-bottom: 6px;
              width: 100%;

              .title {
                width: 80px;
                font-size: 14px;
                margin-right: 20px;
              }

              .link {
                width: 100%;
                display: flex;
                align-items: center;
                height: 40px;
                border-radius: 6px;
                background-color: #fff;
                padding-left: 10px;
                font-size: 14px;
                color: var(--el-color-primary);
              }
            }
          }
        }
      }
    }
  }
</style>
